package controller

import (
	"Goproject/dao"
	"Goproject/models"
	"github.com/gin-gonic/gin"

	"net/http"

	_ "github.com/jinzhu/gorm"
)

func Login(c *gin.Context) {
	c.String(http.StatusOK, "登陆成功!")
}
func Rmlogin(c *gin.Context) {
	c.String(http.StatusOK, "你已经退出了登陆!!")
}
func Register(c *gin.Context) {
	username := c.PostForm("username")

	//获取参数
	password := c.PostForm("password")

	//数据验证
	if len(username) == 0 {
		c.JSON(http.StatusUnprocessableEntity, gin.H{
			"code":    422,
			"message": "用户名不能为空",
		})
		return
	}
	//if len(telephone) != 11 {
	//	ctx.JSON(http.StatusUnprocessableEntity, gin.H{
	//		"code":    422,
	//		"message": "手机号必须为11位",
	//	})
	//	return
	//}
	if len(password) < 6 {
		c.JSON(http.StatusUnprocessableEntity, gin.H{
			"code":    422,
			"message": "密码不能少于6位",
		})
		return
	}

	//判断手机号是否存在
	//var user User
	//db.Where("telephone = ?", telephone).First(&user)
	//if user.ID != 0 {
	//	ctx.JSON(http.StatusUnprocessableEntity, gin.H{
	//		"code":    422,
	//		"message": "用户已存在",
	//	})
	//	return
	//}

	//创建用户
	newUser := models.UserInfo{
		Username: username,
		Password: password,
	}

	dao.DB.Create(&newUser)

	//返回结果
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "注册成功",
	})
}
